package edu.auctionmart

import org.joda.time.DateTime

class EmailService {

	def mailService

	void sendEmail(User user, Auction theAuction) {
		println("ENTERING E-MAIL SERVICE")
		//should send different e-mails depending on whether auction is over or not
		println("Hi, I'm sending an e-mail")
		Auction auction = Auction.get(theAuction.id)
		Set<Bid> bids = auction.getBids()
		DateTime now = DateTime.now()
		DateTime end = auction.getEndTime()

		String winningUsername = ""
		String seller = ""

		if (now >= end) { //auction has ended, current bid should be the winning bid
			println(bids.size())
			auction.setStatus(AuctionStatus.Closed)
			auction.save(flush: true)
			if (auction.getWinner() == null && bids.size() > 0) { //first time setting winner
				println("Setting winner")
				Bid winningBid = auction.getCurrentBid()
				User winningUser = winningBid.getUserAccount()
				auction.setWinner(winningUser)
				auction.save(flush: true)
			}

			if (auction.getWinner() != null) {
				"There's a winner"
				winningUsername = auction.getWinner().getUsername()
				if (user.getUsername() == winningUsername || user.getUsername() == "admin") {
					sendWonMail(auction)
					return
				}
			}

			if (user.getUsername() == auction.getSeller().getUsername()) {
				if (auction.getWinner() != null) {
					sendSoldMail(auction)
					return
				} else {
					sendUnsoldMail(auction)
					return
				}
			}

			//Here the auction ended, the user had an e-mail alert, but they did not win
			sendDidNotWin(auction, user)
			return
		} else { //Informational notification
			sendInfo(auction, user)
			return
		}
	}

	void sendWonMail(Auction auction) {
		auction.refresh()
		String message = "Congrulations, you have won the auction of " + auction.getAuctionTitle() + " for " + "\$" + auction.getCurrentBid().getAmount()
		mailService.sendMail {
			to auction.getWinner().getEmail()
			from "rhgillen@gmail.com"
			subject "Auctionmart: You have won"
			body message
		}
	}

	void sendSoldMail(Auction auction) {
		auction.refresh()
		String message = "Congrulations, sold the auction of " + auction.getAuctionTitle() + " for " + "\$" + auction.getCurrentBid().getAmount()
		mailService.sendMail {
			to auction.getSeller().getEmail()
			from "rhgillen@gmail.com"
			subject "Auctionmart: Your auction sold!"
			body message
		}
	}

	void sendUnsoldMail(Auction auction ) {
		auction.refresh()
		String message = "Unfortunately your auction for " + auction.getAuctionTitle() + " did not sell. We're sorry."
		mailService.sendMail {
			to auction.getSeller().getEmail()
			from "rhgillen@gmail.com"
			subject "Auctionmart: Your auction did not sell"
			body message
		}
	}

	void sendDidNotWin(Auction auction, User user) {
		auction.refresh()
		String message = "Unfortunately you did not win the auction for " + auction.getAuctionTitle() + ". We're sorry."
		mailService.sendMail {
			to user.getEmail()
			from "rhgillen@gmail.com"
			subject "Auctionmart: You did not win"
			body message
		}
	}

	void sendInfo(Auction auction, User user) {
		auction.refresh()
		
		String auctionTitle = auction.getAuctionTitle()
		Float currentPrice = auction?.getCurrentBid()?.getAmount()
		String message
		if (currentPrice) {
			message = "The auction for " + auctionTitle + " is at " + currentPrice + " and " + auction.getCurrentBid().getUserAccount().getUsername() + " is winning"
		} else {
		 message = "The auction for " + auctionTitle + " is at " + auction.getStartBid() + " and there are no bids"
		}
		
		mailService.sendMail {
			to user.getEmail()
			from "rhgillen@gmail.com"
			subject "Auctionmart: Reminder"
			body message
		}
	}
}
